﻿/**
 *	html/monitor/center/device/panel_device_property.js
 *
 *	(C) 2014 Yuanjj
 *
 * 	history:
 * 		2014-09-04		create the file
 */

/**
 *	edit device property
 *
 *	@id					[in] the device id
 *	@panel_center		[in] the center panel
 */
function edit_device_property( id, panel_center )
{
    var form_device_edit 	= panel_center.form_device_edit;

    if ( undefined === form_device_edit )
    {
        form_device_edit 				= _create_device_property_editing_form();
        panel_center.form_device_edit 	= form_device_edit;
    }

    var dev_cmp		= Ext.getCmp( id );
    var record 		= g_device.findRecord( "dev_id", dev_cmp.dev_id );
    var	host		= "all_hosts"
    if ( null === record )
    {
        var count	= g_device.count();
        g_device.insert( count, [[dev_cmp.dev_id]] );
        record 	= g_device.getAt(count);
    }
    else
        host	= record.get( "host" );

    _tunnel_filter( host );
    form_device_edit.loadRecord( record );

    form_device_edit.dev_cmp = dev_cmp;
    form_device_edit.show();
    _enable_windows( false );
}

/**
 *	create device property editting form
 */
var g_form_device_edit;
function _create_device_property_editing_form( )
{
    g_form_device_edit = Ext.create
    ("Ext.form.Panel", {
         title:"编辑设备", frame:true, renderTo:Ext.getBody(),
         draggable:true, constrain:true, simpleDrag:true, floating:true,
         width:311, height:249, resizable:false, buttonAlign:"center",
         listeners:{
             beforeclose:function()
             {
                 form_deviec_edit_cancel_button();
                 return false;
             }
         },
         items:[
             {name:"dev_name", xtype:"textfield", fieldLabel:"设备名称", labelWidth:60, width:300},
             {name:"dev_id", xtype:"textfield", fieldLabel:"设备点号", labelWidth:60, width:300},
             {name:"dev_pos", xtype:"textfield", fieldLabel:"物理位置", labelWidth:60, width:300},
             {name:"machine", xtype:"textfield", fieldLabel:"器件型号", labelWidth:60, width:300},
             {name:"host", xtype:"combobox",	fieldLabel:"主机编号", labelWidth:60, width:300,
                 queryMode:"local", maxHeight:60, store:g_host, valueField:"host", displayField:"host",
                 listeners:{ change:on_host_combox_change },
             },
             {id:"pdevice_edit_tunnel_num", name:"tunnel_num", xtype:"combobox",	fieldLabel:"通道编号", labelWidth:60, width:300, forceSelection:true,
                 queryMode:"local", maxHeight:60, store:g_tunnel_combox, valueField:"number", displayField:"number" },
             {name:"pto_name", xtype:"combobox",	fieldLabel:"设备规约", labelWidth:60, width:300,
                 queryMode:"local", maxHeight:60, store:g_protocal, valueField:"pto_name", displayField:"pto_name" },
         ],
         buttons:[
             {text:"确定", handler:form_deviec_edit_ok_button},
             {text:"取消", handler:form_deviec_edit_cancel_button}
         ]
    });

    return g_form_device_edit;
}

/**
 *	call back for host combox change
 *
 *	@obj			[in] the combox field object
 *	@new_val		[in] the new selected value
 *	@old_val		[in] the old value
 *	@eOpts			[in] the options object passed to Ext.util.Observable.addListener
 */
function on_host_combox_change( obj, new_val, old_val, eOpts )
{
    if ( new_val === old_val || undefined === old_val )
        return;

    _tunnel_filter( new_val );
    var cmp	= Ext.getCmp( "pdevice_edit_tunnel_num" );
    var	rcd	= g_tunnel_combox.getAt( 0 );

    cmp.clearValue();
    if ( undefined !== rcd )
        cmp.setValue( rcd.get("number") );
}

/**
 *	tunnel records filter by host
 *
 *	@host			[in] the host id
 */
function _tunnel_filter( host )
{
    var		res;
    var		index = 0;
    g_tunnel_combox.removeAll();

    g_tunnel.filterBy( function( tnl_rcd )
    {
        if ( "all_hosts" === host )
            res = true;
        else
            res = ( tnl_rcd.get( "host" ) === host );

        if ( true === res )
            g_tunnel_combox.insert( index++, tnl_rcd );
    });
}

/**
 *	the alarm record filter
 *
 *	@dev_rcd		[in] the device record
 */
function _alarm_record_filter( dev_rcd )
{
    var	dev_id = dev_rcd.get( "dev_id" );

    g_alarm_record.filterBy( function( rcd )
    {
        return ( rcd.get( "dev_id" ) === dev_id );
    });
}

/**
 *	select file back
 *
 *	@path			[in] the selected file path
 */
function dev_edit_pto_file_select( path )
{
    var	cmp = Ext.getCmp( "dev_edit_pto_file" );

    cmp.setValue( path );
}

/**
 *	enable windows
 *
 *	@enable			[in] bool flag "true":enable, "false":disable
 */
function _enable_windows( enable )
{
    var	panel_center	= g_panel_main.panel_monitor.panel_center;
    var	panel_west		= g_panel_main.panel_monitor.panel_west;
    var	panel_north		= g_panel_main.panel_monitor.panel_north;

    if ( true !== enable )
    {
        enable_device( "all_devices", false, false );
        panel_center.disable();
        panel_west.disable();
        panel_north.disable();
    }
    else
    {
        enable_device( "all_devices", true, true );
        panel_center.enable();
        panel_west.enable();
        panel_north.enable();
    }
}

/**
 *	buttun "ok" for form device edit
 */
function form_deviec_edit_ok_button()
{
    var dev_cmp				= g_form_device_edit.dev_cmp;
    var values				= g_form_device_edit.getValues();
    var record 				= g_form_device_edit.getRecord();
    var	res;

    if ( record.get( "setup" ) === 1 )
    {
        alert( g_error[0x210] );
        return;
    }

    res = check_form_field_value( dev_cmp, values );
    if ( 0 !== res ) { alert( g_error[res] ); return; }

    dev_cmp.dev_id = values["dev_id"];

    g_form_device_edit.updateRecord();
    if ( record.dirty )
    {
        update_device_record( record );
        record.commit();
    }

    g_form_device_edit.hide();
    _enable_windows( true );
}

function check_form_field_value( dev_cmp, values )
{
    var dev_id			= values["dev_id"];
    var host			= values["host"];
    var tunnel_num		= values["tunnel_num"];
    var pto_name		= values["pto_name"];

    dev_id 			= Ext.String.trim( dev_id );
    host			= Ext.String.trim( host );
    tunnel_num		= Ext.String.trim( tunnel_num );
    pto_name		= Ext.String.trim( pto_name );

    if ( (dev_id !== dev_cmp.dev_id) && (true === _check_device_id( dev_id )) )
        return 0x201;				// function number dup
    if ( "" === dev_id )
        return 0x202;				// function empty
    if ( "" === host )
        return 0x203;				// host emtpy
    if ( "" === tunnel_num )
        return 0x204;				// tunnel number empty
    if ( "" === pto_name )
        return 0x205;				// potocal file empty

    return 0;
}

/**
 *	check if the device with the id is existing
 *
 *	@id		[id] the id of the device
 *
 *	return
 *		true		the device is existing
 *		false		not existing
 */
function _check_device_id( id )
{
    var index = g_device.find( "dev_id", id );

    return (-1 !== index);
}

/**
 *	buttun "cancel" for form device edit
 */
function form_deviec_edit_cancel_button()
{
    g_form_device_edit.hide();
    _enable_windows( true );
}
